//
//  TileView.m
//  Anagrams
//
//  Created by Marin Todorov on 16/02/2013.
//  Copyright (c) 2013 Underplot ltd. All rights reserved.
//

#import "NumberView.h"
#import "config.h"

@interface NumberView ()

@property (weak, nonatomic) IBOutlet UILabel *numberLabel;

@end

@implementation NumberView

+ (NumberView*) emptyNumberView;
{
    return [[NumberView alloc] initWithNumber:0];
}

- (id)init
{
  NSAssert(NO, @"Use initWithLetter:andSideLength instead");
  return nil;
}

//- (id)initWithCoder:(NSCoder *)aDecoder
//{
//  NSAssert(NO, @"Use initWithLetter:andSideLength instead");
//  return nil;
//}

- (id)initWithFrame:(CGRect)frame
{
  NSAssert(NO, @"Use initWithLetter:andSideLength instead");
  return nil;
}

// create new tile for a given number
//0 means empty view
-(instancetype)initWithNumber:(int)number;
{
  //the tile background

  
  //create a new object
    self = [super initWithFrame:CGRectMake(0,0, 60, 60)];
   
  
  if (self != nil) {
    
    //add a number on top if not empty view
      if(number != 0)
      {
          CGFloat frameSizeWidth = self.frame.size.width;
          CGFloat frameSizeHeight = self.frame.size.height;

          UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
          //lblChar.textAlignment = NSTextAlignmentCenter;
          numberLabel.textColor = [UIColor colorWithRed:0.467 green:0.431 blue:0.396 alpha:1.0];
          numberLabel.backgroundColor = [UIColor clearColor];
          numberLabel.text = [NSString stringWithFormat: @"%d", number];
          numberLabel.font = [UIFont systemFontOfSize:kNumberFontSize];
          
          [self addSubview: numberLabel];
          
          [numberLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
          [self setTranslatesAutoresizingMaskIntoConstraints:NO];
          [self removeConstraints:self.constraints];
          [self addConstraint:[NSLayoutConstraint constraintWithItem:numberLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
          [self addConstraint:[NSLayoutConstraint constraintWithItem:numberLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
          [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:frameSizeWidth]];
          [self addConstraint:[NSLayoutConstraint constraintWithItem:self attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:frameSizeHeight]];
          
          
          self.numberLabel.text = [NSString stringWithFormat: @"%d", number];
          self.numberLabel.font = [UIFont systemFontOfSize:kNumberFontSize];
      }
    

    //save the number
    _number = number;
      
    //set the background
       self.backgroundColor = [NumberView colorForNumber: number];
      
    //more initialization here
      
  }
  
  return self;
}

+(UIColor*) colorForNumber:(int) number{
    switch (number) {
        case 2048:
            return [UIColor colorWithRed:0.929 green:0.761 blue:0.18 alpha:2.0];

        case 1024:
            return [UIColor colorWithRed:0.929 green:0.773 blue:0.247 alpha:2.0];
            
        case 512:
            return [UIColor colorWithRed:0.929 green:0.784 blue:0.314 alpha:1.0];
            
        case 256:
            return [UIColor colorWithRed:0.929 green:0.8 blue:0.38 alpha:1.0];
            
        case 128:
            return [UIColor colorWithRed:0.929 green:0.812 blue:0.447 alpha:1.0];
            
        case 64:
            return [UIColor colorWithRed:0.965 green:0.369 blue:0.231 alpha:1.0];
            
        case 32:
            return [UIColor colorWithRed:0.965 green:0.486 blue:0.373 alpha:1.0];
            
        case 16:
            return [UIColor colorWithRed:0.961 green:0.584 blue:0.388 alpha:1.0];
            
        case 8:
            return [UIColor colorWithRed:0.949 green:0.694 blue:0.475 alpha:1.0];
            
        case 4:
            return [UIColor colorWithRed:0.929 green:0.878 blue:0.784 alpha:1.0];
            
        case 2:
            return [UIColor colorWithRed:0.847 green:0.831 blue:0.816 alpha:1.0];
            
        case 0:
            return [UIColor colorWithRed:0.933 green:0.894 blue:0.855 alpha:1.0];
        
        default:
            break;
    }
    
    return [UIColor colorWithRed:1 green:0.761 blue:0.18 alpha:1.0];
}


@end
